<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form
      ref="userForm"
      label-width="220px"
      :rules="rules"
      :model="userInfo"
    >
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 ref="upload"-->
            <UploadImg v-model="userInfo.staffPhoto" />
            <!-- <UploadImg :value="userInfo.staffPhoto" @input="val=>userInfo.staffPhoto = val"/> -->
            <!-- <img :src="userInfo.staffPhoto"> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSave">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employes'
import { getUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {},
      rules: {
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    async loadUserInfoById() {
      const res = await getUserDetailById(this.userId)
      this.userInfo = res.data
      // 当前用户的头像=>显示在子组件UploadImg中
      // this.$refs.upload.imageUrl = this.userInfo.staffPhoto
    },
    hSave() {
      // 兜底校验
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.doEdit()
        }
      })
    },
    // 做修改
    async doEdit() {
      try {
        // 更新头像地址
        // this.userInfo.staffPhoto = this.$refs.upload.imageUrl
        await saveUserDetailById(this.userInfo)
        this.$message.success('修改成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
